import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * The Vertex class stores its name and
 * a mark for whether its visited or not
 * also a hashMap which holds its neighbors
 * @author ldong
 *
 */
public class Vertex
{
	
	private String name;
	private boolean mark;
    private HashMap<Vertex, Integer> adjacentVertexWeightTable;
	private ArrayList<Vertex> arrayList;

	/**
	 * Constructor
	 */
	public Vertex()
	{
		this("");
		
	}
	
	/**
	 * Second constructor 
	 * @param name
	 */
	public Vertex(String name)
	{
		arrayList = new ArrayList<Vertex>();
		this.name = name;
		adjacentVertexWeightTable = new HashMap<Vertex, Integer>();
	    mark = false;	
	}
	
	/**
	 * Add its corresponding weight to its adjacent vertex
	 * @param adj
	 * @param weight
	 * @return
	 */
	public boolean addAdjacentWeight(Vertex adj, int weight)
	{
    	// if it does not contain adj and not null
        if( adj != null && !adjacentVertexWeightTable.containsKey(adj))
		{
			  adjacentVertexWeightTable.put(adj, weight);
			  arrayList.add(adj);
              return true;
	    }
	    else
	    	return false;
	    	
	}

	/**
	 * Get the weight from its neighbors
	 * @param v
	 * @return
	 */
    public Integer findWeightFrom(Vertex v) 
    {
    	if(adjacentVertexWeightTable.containsKey(v))
    	{
    		return adjacentVertexWeightTable.get(v);
    	}
    	else
    		return null; // means it doesnt exist
    }
    
	/**
	 * Overriding the toString method
	 */
	public String toString()
	{
		
	   String temp = name + " : ";
	   
       Set<Map.Entry<Vertex, Integer>> entrySet = adjacentVertexWeightTable.entrySet();
       
          for(Map.Entry<Vertex, Integer> map : entrySet )
          {	  
    	     temp += ("<" + map.getKey().getName() 
    	    		 + ", " + map.getValue()+ "> ");
    	  } // end of for loop
       
		
		return temp;
	}

	
	/********************************************************
	 *                Setters and getters                   *
	 ********************************************************/
    
	/**
	 * Get the set of Vertex
	 */
	public Set<Vertex> getVertexSet() 
	{
		return  adjacentVertexWeightTable.keySet();
	}
	
	public ArrayList<Vertex> getVertexList()
	{
		return arrayList;
	}
	
	/**
     * getter for name
     * @return
     */
	public String getName()
	{
		return name;
	}

	/**
	 * setter for name
	 * @param name
	 */
	public void setName(String name)
	{
		this.name = name;
	}

	/**
	 * See if the vertex is visited or not
	 * @return
	 */
	public boolean isVisited()
	{
		return mark;
	}	

	 /**
	  * Set the mark visited or not
	  * @param mark
	  */
	public void setMark(boolean mark)
	{
		this.mark = mark;
	}

	/**
	 * Getter for getAdjacentVertexWeightTable
	 * @return
	 */
	public HashMap<Vertex, Integer> getAdjacentVertexWeightTable() {
		return adjacentVertexWeightTable;
	}
	
	/**
	 * Setter for getAdjacentVertexWeightTable
	 */
	public void setAdjacentVertexWeightTable(
			HashMap<Vertex, Integer> adjacentVertexWeightTable) {
		this.adjacentVertexWeightTable = adjacentVertexWeightTable;
	}

}
